package com.test.controller;

import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.test.form.ValidationForm;

@Controller
@RequestMapping("/validationform.html")
public class ValidationController {
	
	private static final Logger logger = LoggerFactory.getLogger(ValidationController.class);
	
	@RequestMapping(method = RequestMethod.GET)
	public String showValidationForm(Map model){
		logger.info("showValidationForm");
		
		ValidationForm validationForm = new ValidationForm();
		model.put("validationForm", validationForm);
		return "validationform";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String provessValidationForm(Map model, @Valid ValidationForm validationForm,
			BindingResult result ) {
		logger.info("provessValidationForm");
		logger.info(result.toString());
		logger.info(validationForm.toString());
		
		if (result.hasErrors()) {
			return "validationform";
		}
		
		model.put("validationForm", validationForm);
		return "validationsuccess";
	}

}
